package com.yanqu.road.server.manager.union;

import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.manger.IUserBaseInfoSyncListener;
import com.yanqu.road.utils.string.StringUtils;

public class UnionMemberUserBaseSyncListener implements IUserBaseInfoSyncListener {

    @Override
    public void notifySync(UserBaseInfo userBaseInfo, long userId) {
        CrossUnionMember member = CrossUnionMgr.getUnionMember(userBaseInfo.getUnionUid(), userId);
        if(member != null){
            boolean earnChange = false;
            if(member.getUserBaseInfo() != null){
                if(userBaseInfo.getEarnSpeed().compareTo(member.getUserBaseInfo().getEarnSpeed()) != 0){
                    earnChange = true;
                }
            }

            // 判断会长 vip 设置是否改变
            boolean hideVipChange = false;
            if (member.getPosition() == eUnionPosition.Master.getValue()) {
                int oldValue = member.getUserBaseInfo().getHideVipAndAddress();
                int newValue = userBaseInfo.getHideVipAndAddress();
                if (oldValue != newValue) {
                    hideVipChange = true;
                }
            }

            member.setUserBaseInfo(userBaseInfo);
            if(earnChange && !StringUtils.isNullOrEmpty(member.getUnionUid())){
                UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(member.getUnionUid());
                CrossUnionMgr.calcUnionEarnSpeed(unionInfo);
                if(unionInfo != null){
                    if(unionInfo.getMasterUserId() == member.getUserId()){
                        unionInfo.setMasterNickName(userBaseInfo.getNickName());
                        unionInfo.setVipExp(userBaseInfo.getVipExp());
                    }
                }
            }

            if (hideVipChange) {
                CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(member.getUnionUid());
                if (unionInfo != null) {
                    CrossUnionMgr.sendUnionChangeBroadcast(eUnionSyncType.Normal.getValue(), unionInfo, null, null, 0);
                }
            }
        }
    }
}
